package com.wzdigit.wms.wms.service.basic;

import cn.hutool.core.io.FileUtil;
import com.wzdigit.wms.wms.constant.FilePathConstant;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.*;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FileService {


    public String  saveFile(JasperPrint jasperPrint,String fileName,String sufix) throws JRException {
        String basePath = FilePathConstant.BASE_PATH ;
        if(!FileUtil.exist(basePath)){
            FileUtil.mkdir(basePath);
        }
        long time = new Date().getTime();
        String lastName = fileName +"_"+time+"."+sufix;
        String absolutePath   = basePath + lastName;
        JRXlsxExporter exporter = new JRXlsxExporter();
        //设置输入项
        ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
        exporter.setExporterInput(exporterInput);
        OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(absolutePath);
        exporter.setExporterOutput(exporterOutput);
        exporter.exportReport();
        return  lastName;
    }

    public String  saveFile(List<ExporterInputItem> jasperPrint, String fileName, String sufix) throws JRException {
        String basePath = FilePathConstant.BASE_PATH ;
        if(!FileUtil.exist(basePath)){
            FileUtil.mkdir(basePath);
        }
        long time = new Date().getTime();
        String lastName = fileName +"_"+time+"."+sufix;
        String absolutePath   = basePath + lastName;
        JRXlsxExporter exporter = new JRXlsxExporter();
        //设置输入项
        ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
        exporter.setExporterInput(exporterInput);
        OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(absolutePath);
        exporter.setExporterOutput(exporterOutput);
        exporter.exportReport();
        return  lastName;
    }

}
